package com.zhong.auth.domain.service.impl;

import com.alibaba.fastjson.JSON;
import com.zhong.auth.common.enums.AuthUserStatusEnum;
import com.zhong.auth.common.enums.IsDeleteFlagEnum;
import com.zhong.auth.domain.bo.AuthPermissionBO;
import com.zhong.auth.domain.convert.AuthPermissionBOConvert;
import com.zhong.auth.domain.service.AuthPermissionDomainService;
import com.zhong.auth.infra.basic.entity.AuthPermission;
import com.zhong.auth.infra.basic.service.AuthPermissionService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

@Slf4j
@Service
public class AuthPermissionDomainServiceImpl implements AuthPermissionDomainService {
    @Resource
    private AuthPermissionService authPermissionService;

    @Override
    public Boolean add(AuthPermissionBO authPermissionBO) {
        if (log.isInfoEnabled()) {
            log.info("AuthPermissionDomainServiceImpl.add.authPermissionBO: {}", JSON.toJSONString(authPermissionBO));
        }
        AuthPermission authPermission = AuthPermissionBOConvert.INSTANCE.convertBOToEntity(authPermissionBO);
        authPermission.setIsDelete(IsDeleteFlagEnum.UN_DELETE.getCode());
        authPermission.setStatus(AuthUserStatusEnum.OPEN.getCode());
        Integer count = authPermissionService.insert(authPermission);
        return count > 0;
    }

    @Override
    public Boolean update(AuthPermissionBO authPermissionBO) {
        if (log.isInfoEnabled()) {
            log.info("AuthPermissionDomainServiceImpl.update.authPermissionBO: {}", JSON.toJSONString(authPermissionBO));
        }
        AuthPermission authPermission = AuthPermissionBOConvert.INSTANCE.convertBOToEntity(authPermissionBO);
        Integer count = authPermissionService.update(authPermission);
        return count > 0;
    }

    @Override
    public Boolean delete(AuthPermissionBO authPermissionBO) {
        if (log.isInfoEnabled()) {
            log.info("AuthPermissionDomainServiceImpl.delete.authPermissionBO: {}", JSON.toJSONString(authPermissionBO));
        }
        AuthPermission authPermission = AuthPermission.builder()
                .id(authPermissionBO.getId())
                .isDelete(IsDeleteFlagEnum.DELETE.getCode())
                .build();
        Integer count = authPermissionService.update(authPermission);
        return count > 0;
    }
}
